﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Ewts.Storage
{
    public partial class Weight : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["cjoid"] != null && Request["cjoid"].ToString().Trim() != "")
            {
                Binddata(int.Parse(Request["cjoid"].ToString().Trim()));

            }
            if (Request["wrlid"] != null && Request["wrlid"].ToString().Trim() != "")
            {
                BindRept(Request["wrlid"].ToString().Trim());
            }
        }

        private void Binddata(int cjoid)
        {
            DataLib.Model.CJOItem model = new DataLib.BLL.CJOItem().GetListByCid(cjoid);
            tdCjo.InnerText = model.cjono;
            tdCumCode.InnerText = model.customercode;
            tdCumName.InnerText = model.companyname;
            tdLoadNum.InnerText = model.loadnumno;
            tdValidate.InnerText = DateTime.Now.ToString("yyyy-MM-dd");
            tdWeightTotal.InnerText = model.totalWeight.ToString();
            hidcjoid.Value = model.cjoid.ToString();
            hidcumid.Value = model.customerid.ToString();
            hidloadid.Value = model.loadnumid.ToString();
            
         }

        protected void BindRept(string wrlid)
        {
            rept.DataSource = new DataLib.BLL.WRLItems().GetList("wrlid=" + wrlid).Tables[0];
            rept.DataBind();
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            DataLib.Model.LoadNum loadmodel = new DataLib.Model.LoadNum();
            DataLib.Model.Wrl wrlmodel = new DataLib.Model.Wrl();
            DataLib.Model.WRLItems itemmodel = new DataLib.Model.WRLItems();
            wrlmodel.cjoid = int.Parse(hidcjoid.Value);
            wrlmodel.costomercode = tdCumCode.InnerText;
            wrlmodel.companyname = tdCumName.InnerText;
            wrlmodel.customid = int.Parse(hidcumid.Value);
            wrlmodel.loadnumid = int.Parse(hidloadid.Value);

            itemmodel.barcode = txtBarCode.Value.Trim();
            itemmodel.dupllcateno = txtDupllcateno.Value.Trim();
            itemmodel.GoodsCode = txtGoodsCode.Value.Trim();
            itemmodel.GoodsName = txtGoodsName.Value.Trim();
            itemmodel.Gwt = decimal.Parse(txtGwt.Value.Trim());
            itemmodel.Nwt = decimal.Parse(txtNwt.Value.Trim());
            itemmodel.packageno = txtPackageno.Value.Trim();
            itemmodel.Qty = int.Parse(txtQty.Value.Trim());
            itemmodel.WrlID = new DataLib.BLL.Wrl().Add(wrlmodel);

            loadmodel.totalWeight += itemmodel.Nwt;
            new DataLib.BLL.LoadNum().Update(loadmodel);

            SetVehicleArrange(wrlmodel.cjoid);

            if (new DataLib.BLL.WRLItems().Add(itemmodel) > 0)
            {
                string url = "";
                if (Request["wrlid"] != null && Request["wrlid"].ToString().Trim() != "")
                {

                }
                DataLib.Common.MessageBox.ShowAndRedirect(Page, "保存成功","");
            }
        }

        /// <summary>
        /// 根据cjoid对派车状态进行改变
        /// </summary>
        /// <param name="cjoid">CJO单的ID号</param>
        protected void SetVehicleArrange(int cjoid)
        {
            DataLib.Model.VehicleArrange vamodel = new DataLib.BLL.VehicleArrange().GetModelByCjoid(cjoid);

            //派车操作
            if (vamodel != null && vamodel.status == 1)
            {
                vamodel.status = 0;

                string[] arrplate = vamodel.platenumberid.Split(',');
                int i = 0;
                for (i = 0; i < arrplate.Length; i++)
                {
                    if (arrplate[i] != null && arrplate[i].ToString().Trim() != "")
                    {
                        int plateid = int.Parse(arrplate[i]);
                        DataLib.Model.PlateNumber platemodel = new DataLib.BLL.PlateNumber().GetModel(plateid);
                        platemodel.currentstatus--;
                        new DataLib.BLL.PlateNumber().Update(platemodel);
                    }
                }

                //收货员操作
                string[] arrvehider = vamodel.vehiderid.Split(',');
                for (i = 0; i < arrvehider.Length; i++)
                {
                    if (arrvehider[i] != null && arrvehider[i].ToString().Trim() != "")
                    {
                        int vehiderid = int.Parse(arrvehider[i]);
                        DataLib.Model.Vehiclor vehmodel = new DataLib.BLL.Vehiclor().GetModel(vehiderid);
                        vehmodel.currentstatus--;
                        new DataLib.BLL.Vehiclor().Update(vehmodel);
                    }
                }
            }
        }
    }
}